<?php
class ModelToolMessager extends Model {
    private  $client_key = '515a455c86ba00d2264553b427fa3c43';
    private  $secrect_key = '6e35417f7f3a09a9903a37d37d9c5cb5';
    private  $device_id = '1234';
    public function sendCode($phone,$code)
    {
        $result = $this->sendMessage($phone,$code);
        if($result->msgCode == 301)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    protected function sendMessage($phone,$code)
    {
        $curl = curl_init();
        $base_url = 'http://sms.user.noahedu.com/uc/noahpublic/anonymous/sendvalidatecode?';
        $rand =$this->create_uuid();
        $str =  'GEThttp://sms.user.noahedu.com/uc/noahpublic/anonymous/sendvalidatecodeclient_key='.$this->client_key.'code='.$code.'device_id='.$this->device_id.'rand='.$rand.'telephone='.$phone.'type=rg'.$this->secrect_key;
        $sign = md5($str);
        $request_paras = "sign=".$sign."&client_key=".$this->client_key."&device_id=".$this->device_id."&code=".$code."&rand=".$rand."&type=rg&telephone=".$phone;
        curl_setopt($curl, CURLOPT_URL, $base_url.$request_paras);
        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
        $response = curl_exec($curl);
        curl_close($curl);
        $result = json_decode($response);
        return $result;
    }

    protected function create_uuid($prefix = ""){    //可以指定前缀
        $str = md5(uniqid(mt_rand(), true));
        $uuid  = substr($str,0,8) ;
        $uuid .= substr($str,8,4) ;
        $uuid .= substr($str,12,4) ;
        $uuid .= substr($str,16,4);
        $uuid .= substr($str,20,12);
        return $prefix . $uuid;
    }
}


